<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts';
import resize from './mixins/resize';
require('echarts/theme/macarons'); // echarts theme

// const animationDuration = 3000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '300px',
    },
    tenantWaitSends: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    tenantWaitSends(n) {
      // console.log(this.tenantWaitSends, 'tenantWaitSends');
      if (n) {
        this.initChart(n);
      }
    },
  },
  methods: {
    initChart(tenantWaitSends) {
      this.chart = echarts.init(this.$el, 'macarons');
      console.log(tenantWaitSends);
      // 过滤派遣人数为0的
      // 按照派遣人数排序
      // 取迁5个
      const formatCount = tenantWaitSends
        .filter(item => item.sendCount > 0)
        .sort((a, b) => a.sendCount - b.sendCount)
        .slice(0, 5)
        .map(item => ({ ...item, value: item.sendCount }));

      this.chart.setOption({
        title: {
          text: '',
          subtext: '',
          left: 'center',
        },
        tooltip: {
          trigger: 'item',
        },
        legend: {
          orient: 'vertical',
          left: 'left',
        },
        series: [
          {
            name: '用工单位派遣情况',
            type: 'pie',
            radius: '50%',
            data: formatCount,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
            },
          },
        ],
      });
    },
  },
};
</script>
